/**
 * 计时器
 * @author zzhanglin
 * @param _delay 延迟多少毫秒触发一次
 * @param _repeatCount 总共的次数，为0的话就一直循环，默认为0。
 * @returns {Timer}
 */
Timer = function( delay , repeatCount )
{
	EventDispatcher.call(this);
	
	this.delay = delay? delay : 25  ; //计时器事件间的延迟（以毫秒为单位）。 
	this.repeatCount = repeatCount?repeatCount : 0 ; //设置的计时器运行总次数
	
	this._running = false ;
	this._currentCount = 0 ;
	this._intervalId = 0 ;
};
FLASH.extend( Timer , EventDispatcher );

//=========getter/setter==============================

//是否在运行
Timer.prototype.isRunning = function(){ return this._running ; } ;
//计时器从 0 开始后触发的总次数
Timer.prototype.getCurrentCount = function(){ return this._currentCount ; };

//===============================================

/**
 * 重置
 */
Timer.prototype.reset = function()
{
	this._currentCount = 0 ;	
	this._running = false ;
};

/**
 * 启动
 */
Timer.prototype.start = function(){
	this.reset();
	
	var _self = this ;
	this._currentCount=0 ;
	this._intervalId = setInterval(loop,_self.delay);
	function loop(){
		if(_self.repeatCount>0){
			_self._currentCount++;
			_self.dispatchEvent( new TimerEvent(TimerEvent.TIMER) );
			if(_self._currentCount == _self.repeatCount ){
				_self.stop();
				_self.dispatchEvent( new TimerEvent(TimerEvent.TIMER_COMPLETE) );
				return ;
			}
		}
	}
};

/**
 * 停止
 */
Timer.prototype.stop = function()
{
	this._running = false ;
	var _self = this ;
	clearInterval(_self._intervalId);
};